var app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    inputShowed: false,
    inputVal: "",
    flag: 0,
    pagenum: 1, //分页数据
    pagenum1: 1, //分页数据
    pagenum2: 1, //分页数据
    //品牌变量组
    pinpai: [], //品牌所有name的数组
    logos: [], //logo数组
    pinpaizu: [], //品牌name和logo数组
    pinpaiIndex: 1, //品牌索引
    pinpaiIds: [], //品牌所有id的数组
    pinpaiId: 0, //品牌id
    pinpaiName: "", //品牌名字
    //车系变量组
    chexi: [], //车系所有name的数组
    chexiIndex: 1, //车系索引
    chexiIds: [], //车系所有id的数组 
    chexiId: 0, //车系id
    chexiName: "", //车系名字
    //车型变量组
    chexing: [], //车型所有name的数组
    chexingIndex: 1, //车型索引
    chexingIds: [], //车型所有id的数组
    chexingId: 0, //车型id
    chexingName: "", //车型名字
  },
  d: {
    flag: true
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取品牌的ajax
    this.getpinpai();
  },
  onShow(){
    // 判断是否登录
    app.getUserInfoFun();
  },
  /**
   * 获取品牌的ajax
   */
  getpinpai(pagenum, inputVal, pinpai, pinpaiIds, logos,pinpaizu) {
    var url = "obd/car/selectCaropList";
    var param = {
      p_id: 0,
      pagenum: pagenum ? pagenum : this.data.pagenum,
      name: inputVal ? inputVal : ''
    }
    app.getRequest(url, param, res => {
      if (res.code == 200) {
        pinpai = pinpai ? pinpai : this.data.pinpai;
        var pinpais = res.data.list;
        pinpaiIds = pinpaiIds ? pinpaiIds : this.data.pinpaiIds;
        logos = logos ? logos : this.data.logos;
        pinpaizu = pinpaizu ? pinpaizu : this.data.pinpaizu;;
        for (var i = 0; i < pinpais.length; i++) {
          logos.push(pinpais[i].LOGO_PATH);
          pinpai.push(pinpais[i].NAME);
          pinpaiIds.push(pinpais[i].ID);
          pinpaizu.push({
            NAME: pinpais[i].NAME,
            LOGO_PATH: app.globalData.globalUrl + pinpais[i].LOGO_PATH
          })
        }
        this.setData({
          logos: logos,
          pinpai: pinpai,
          pinpaiIds: pinpaiIds,
          pinpaizu: pinpaizu
        });
        this.d.flag = true;
      } else {
        wx.showToast({
          title: res.message
        })
      }

    });
  },
  /**
   * 搜索框函数组
   */
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputVal: "",
      inputShowed: false
    });
    this.setData({
      pinpai: [],
      pinpaizu: []
    });
    if (this.data.flag == 0) {
      this.getpinpai(1);
    } else if (this.data.flag == 1) {
      this.getchexi(this.data.e, 1)
    } else if (this.data.flag == 2) {
      this.getchexing(this.data.e, 1)
    }

  },
  clearInput: function () {
    this.setData({
      inputVal: ""
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value
    });
  },
  // 点击数字键盘确定 进行搜索
  searchBtn() {
    if (this.data.flag == 0) {
      this.getpinpai(1, this.data.inputVal, [], [], [], [])
    } else if (this.data.flag == 1) {
      this.getchexi(this.data.e, 1, this.data.inputVal, [], [])
    } else if (this.data.flag == 2) {
      this.getchexing(this.data.e, 1, this.data.inputVal, [], [])
    }

  },
  //品牌的ajax
  getchexi(e, pagenum1, inputVal, chexi, chexiIds) {
    //获取品牌的索引值
    var ref = parseInt(e.currentTarget.dataset.index);
    this.setData({
      pinpaiIndex: ref,
      pinpaiId: this.data.pinpaiIds[ref],
      pinpaiName: this.data.pinpai[ref],
      logo: this.data.logos[ref]
    });
    //logo
    wx.setStorageSync("logo", this.data.logo);
    //默认头像
    wx.setStorageSync(
      "defaultCar_img",
      app.globalData.globalUrl + this.data.logo
    );
    //品牌id
    wx.setStorageSync(
      "b_id", this.data.pinpaiId
    );
    //品牌name
    wx.setStorageSync(
      "pinpaiName", this.data.pinpaiName
    );

    var thisID = this.data.pinpaiIds[ref];
    thisID = thisID - 0;
    var url = "obd/car/selectCarop";
    var data = {
      p_id: thisID,
      pagenum1: pagenum1 ? pagenum1 : this.data.pagenum1,
      name: inputVal ? inputVal : ''
    };
    app.getRequest(url, data, res => {
      chexi = chexi ? chexi : this.data.chexi;
      var chexis = res.data;
      chexiIds = chexiIds ? chexiIds : this.data.chexiIds;
      for (var i = 0; i < chexis.length; i++) {
        chexi.push(chexis[i].NAME);
        chexiIds.push(chexis[i].ID);
      }
      this.setData({
        chexi: chexi,
        chexiIds: chexiIds,
        flag: 1
      });
    });
  },
  bindPinpaiChange(e) {
    this.getchexi(e);
    this.setData({
      e: e,
    });
  },
  //车系的ajax
  getchexing(e, pagenum2, inputVal, chexing, chexingIds) {
    //获取车系的索引值
    var ref = parseInt(e.currentTarget.dataset.index);
    this.setData({
      chexiIndex: ref,
      chexiId: this.data.chexiIds[ref],
      chexiName: this.data.chexi[ref]
    });
    //车系id
    wx.setStorageSync(
      "t_id", this.data.chexiId
    );
    //车系name
    wx.setStorageSync(
      "chexiName", this.data.chexiName
    );
    var thisID = this.data.chexiIds[ref];
    thisID = thisID - 0;
    var url = "obd/car/selectCaropList";
    var data = {
      p_id: thisID,
      pagenum: pagenum2 ? pagenum2 : this.data.pagenum2,
      name: inputVal ? inputVal : ''
    };
    app.getRequest(url, data, res => {
      chexing = chexing ? chexing : this.data.chexing;
      var chexings = res.data.list;
      chexingIds = chexingIds ? chexingIds : this.data.chexingIds;
      for (var i = 0; i < chexings.length; i++) {
        chexing.push(chexings[i].NAME);
        chexingIds.push(chexings[i].ID);
      }
      this.setData({
        chexing: chexing,
        chexingIds: chexingIds,
        flag: 2
      });
    });
  },
  bindChexiChange: function (e) {
    this.getchexing(e);
    this.setData({
      e: e,
    });
  },
  //车型的ajax
  bindChexingChange: function (e) {
    //获取车型的索引值
    var ref = parseInt(e.currentTarget.dataset.index);
    this.setData({
      chexingIndex: ref,
      chexingId: this.data.chexingIds[ref],
      chexingName: this.data.chexing[ref]
    });
    //车型id
    wx.setStorageSync(
      "m_id", this.data.chexingId
    );
    //车型name
    wx.setStorageSync(
      "chexingName", this.data.chexingName
    );
    wx.navigateBack()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.d.flag) {
      this.d.flag = false;
      if (this.data.flag == 0) {
        this.setData({
          pagenum: this.data.pagenum + 1
        });
        // 重新加载品牌
        this.getpinpai(this.data.pagenum, this.data.inputVal);
      } else if (this.data.flag == 1) {
        this.setData({
          pagenum1: this.data.pagenum1 + 1
        });
        // 重新加载车系
        this.getchexi(this.data.e);
      } else if (this.data.flag == 2) {
        this.setData({
          pagenum2: this.data.pagenum2 + 1
        });
        // 重新加载车型
        this.getchexing(this.data.e);
      }

    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {}
});